package com.xhd.server;

import java.io.File;
import java.io.FileInputStream;
import java.security.MessageDigest;

import com.xhd.base.log.Log;
import com.xhd.base.log.LogFactory;

public class XMD5 {
	static final char[] jm195 = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
	  private static final Log jm9;
	  static Class jm10;

	  static
	  {
	    Class tmp100_97 = jm10;
	    if (tmp100_97 == null)
	    {
	      
	      try
	      {
	        XMD5.jm10 = Class.forName("com.xhd.util.XMD5");
	      }
	      catch (ClassNotFoundException localClassNotFoundException)
	      {
	        throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
	      }
	    }
	    jm9 = LogFactory.getLog(tmp100_97);
	  }

	  public String getFileMD5(File paramFile)
	  {
	    try
	    {
	      if ((paramFile != null) && (paramFile.exists()))
	      {
	        byte[] arrayOfByte = new byte[4096];
	        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
	        FileInputStream localFileInputStream = new FileInputStream(paramFile);
	        int i = 0;
	        while (true)
	        {
	          i = localFileInputStream.read(arrayOfByte);
	          if (i <= 0)
	            break;
	          localMessageDigest.update(arrayOfByte, 0, i);
	        }
	        localFileInputStream.close();
	        localFileInputStream = null;
	        arrayOfByte = (byte[])null;
	        return toHexString(localMessageDigest.digest());
	      }
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error(localThrowable);
	    }
	    return null;
	  }

	  public String getFileMD5(String paramString)
	  {
	    return getFileMD5(new File(paramString));
	  }

	  public String getMD5(byte[] paramArrayOfByte)
	  {
	    try
	    {
	      if ((paramArrayOfByte != null) && (paramArrayOfByte.length > 0))
	      {
	        MessageDigest localMessageDigest = MessageDigest.getInstance("MD5");
	        localMessageDigest.update(paramArrayOfByte);
	        return toHexString(localMessageDigest.digest());
	      }
	    }
	    catch (Throwable localThrowable)
	    {
	      jm9.error(localThrowable);
	    }
	    return null;
	  }

	  public static void main(String[] paramArrayOfString)
	  {
	    XMD5 localCPCMD5 = new XMD5();
	    String str = "test";
	    if ((paramArrayOfString != null) && (1 == paramArrayOfString.length))
	      str = paramArrayOfString[0];
	    System.out.println(str + " : " + localCPCMD5.getMD5("test".getBytes()));
	  }

	  public String toHexString(byte[] paramArrayOfByte)
	  {
	    StringBuffer localStringBuffer = new StringBuffer(paramArrayOfByte.length * 2);
	    for (int i = 0; i < paramArrayOfByte.length; ++i)
	    {
	      localStringBuffer.append(jm195[(paramArrayOfByte[i] >> 4 & 0xF)]);
	      localStringBuffer.append(jm195[(paramArrayOfByte[i] & 0xF)]);
	    }
	    return localStringBuffer.toString();
	  }
}
